﻿
using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;

namespace DeviceManager.ContentPage
{
    /// <summary>
    /// PerformancePage.xaml 的交互逻辑
    /// </summary>
    public partial class PerformancePage : Page
    {
        public PerformancePage()
        {
            InitializeComponent();
            this.Loaded += PerformancePage_Loaded;
        }

        private void PerformancePage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                RefSumChartInfo();
            }
        }

        //去水印
        void chart_Rendered(object sender, EventArgs e)
        {
            var c = sender as Chart;
            var legend = c.Legends[0];
            var root = legend.Parent as Grid;
            //直接将其移除掉
            try
            {
                root.Children.RemoveAt(10);
                root.Children.RemoveAt(8);
            }
            catch { }
        }


        /// <summary>
        /// 刷新第一个饼图数据
        /// </summary>
        internal void RefSumChartInfo()
        {
            //离线数
            int offLineCount = ManagerDBCtrl.GetMng().GetOffLineDeviceCount();
            //维护数
            int maintainCount = ManagerDBCtrl.GetMng().GetMaintainDeviceCount();
            //告警数
            int warringCount = ManagerDBCtrl.GetMng().GetWarrDeviceCount();
            //总设备数
            int deviceCount = ManagerDBCtrl.GetMng().GetDeviceCount();
            int noneDeviceCount = deviceCount - offLineCount - maintainCount - warringCount;
            Title title = new Visifire.Charts.Title();
            title.Text = "设备运行状态概要";
            chart_1.Titles.Clear();
            chart_1.Titles.Add(title);
            chart_1.Series.Clear();

            DataSeries ds = new DataSeries();
            ds.RenderAs = RenderAs.Pie;
            ds.LabelStyle = LabelStyles.Inside;
            ds.LabelEnabled = true;

            ds.DataPoints.Add(new DataPoint() { AxisXLabel = "离线数量", YValue = offLineCount });
            ds.DataPoints.Add(new DataPoint() { AxisXLabel = "维护数量", YValue = maintainCount });
            ds.DataPoints.Add(new DataPoint() { AxisXLabel = "告警数量", YValue = warringCount });
            ds.DataPoints.Add(new DataPoint() { AxisXLabel = "正常数量", YValue = noneDeviceCount });
            chart_1.Series.Add(ds);


            //读取各个类型设备的详细数据
            //告警设备
            List<DeviceNode> nodes = ManagerDBCtrl.GetMng().GetAllDevices(@"(
not(DeviceVoltage between DeviceMinVoltage and DeviceMaxVoltage
and DeviceElectricCurrent between DeviceMinElectricCurrent and DeviceMaxElectricCurrent
and DeviceTemperature between DeviceMinTemperature and DeviceMaxTemperature
and DeviceHumidity between DeviceMinHumidity and DeviceMaxHumidity)
and datediff(MINUTE, DeviceLastUpDate, GETDATE()) <= DeviceUpDateTime
and DeviceIsMaintain = 0)");

            lv_waringdevices.ItemsSource = nodes;

            //离线设备
            nodes = ManagerDBCtrl.GetMng().GetAllDevices("(datediff(MINUTE,DeviceLastUpDate,GETDATE())>DeviceUpDateTime  and DeviceIsMaintain = 0)");
            lv_offlinedevices.ItemsSource = nodes;

            //维护设备
            nodes = ManagerDBCtrl.GetMng().GetAllDevices("( DeviceIsMaintain = 1)");
            lv_maindevices.ItemsSource = nodes;


        }
    }
}
